////
////  DetailHeaderView.swift
////  HengYouCai
////
////  Created by lwj on 2017/11/10.
////  Copyright © 2017年 lwj. All rights reserved.

import UIKit

class DetailHeaderView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = ColorFromRGBHex_WL(0x3d3d3d)
        
        
        self.initUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func setUpHeaderView(_ detailModel : DetailModel ) {
        
        titleLab.text = detailModel.profitLabel
        bottomLab.text = detailModel.dateTips
        
        todayLab.text = detailModel.investDateLabel2
        jixiLab.text = detailModel.beginDateLabel2
        huikuanLab.text = detailModel.endDateLabel2
        
        let productModel : ProjectModel = detailModel.products
        
        self.huikuanValueLab.text = detailModel.endDateLabel
        self.jixiValueLab.text = detailModel.beginDateLabel
        self.todayValueLab.text = detailModel.investDateLabel
        
        if productModel.type == "-1" {
            self.cashImg.image = UIImage(named: "")
            
            self.profitLab.attributedText = handlerProfit(productModel.minProfit,"")
            
        }else{
            if productModel.cashRateShow == "1"{
                self.cashImg.image = UIImage(named: "cashProfit")
                
                self.profitLab.attributedText = handlerProfit(productModel.profit, productModel.cashRateProfit)
            }else{
                self.cashImg.image = UIImage(named: "platformProfit")
                
                self.profitLab.attributedText = handlerProfit(productModel.profit, productModel.profitFloat)
            }
            
            
        }
        
        self.tagsView.tagsStr(productModel.tags)
        _ = tagsView.sd_resetLayout()
            .centerXEqualToView(self)?
            .offset(-tagsView.totoalWidth/2)?
            .topSpaceToView(titleLab,18)?
            .heightIs(15)
        let titleOneLab : UILabel = self.viewWithTag(1001)?.viewWithTag(10001) as! UILabel
        let titleTwoLab : UILabel = self.viewWithTag(1002)?.viewWithTag(10001) as! UILabel
        let titleThreeLab : UILabel = self.viewWithTag(1003)?.viewWithTag(10001) as! UILabel
        let valueOneLab : UILabel = self.viewWithTag(1001)?.viewWithTag(10002) as! UILabel
        let valueTwoLab : UILabel = self.viewWithTag(1002)?.viewWithTag(10002) as! UILabel
        let valueThreeLab : UILabel = self.viewWithTag(1003)?.viewWithTag(10002) as! UILabel
        
        if productModel.type == "0" {
            titleOneLab.text = detailModel.plstimeLimitLabel //"投资期限"
            titleTwoLab.text = detailModel.surplusAmountLabel //"剩余金额"
            titleThreeLab.text = detailModel.productsScaleLabel //"项目总额"
            valueOneLab.attributedText = handlerDataAndMoney(String(format: "%.f", productModel.plstimeLimitValue.cGFloat), productModel.plstimeLimitType == "0" ? "天" : "个月")
            valueTwoLab.attributedText =  handlerMoney(NSString(moneyNoPointAndYuan: productModel.surplusAmount) as String)
            valueThreeLab.attributedText = handlerMoney(String(format: "%.2f万", Double(productModel.productsScale)!/10000))
//            valueThreeLab.attributedText =  handlerMoney(NSString(moneyNoPoint: productModel.productsScale) as String)
            
            
            
        }else{
            titleOneLab.text = detailModel.cashRateLabel2 //"起投金额"
            titleTwoLab.text = detailModel.plstimeLimitLabel //"投资期限"
            titleThreeLab.text = detailModel.investEndLabel //"投资限额"
            
            valueOneLab.attributedText = handlerDataAndMoney(String(format: "%.f", productModel.investBegin.cGFloat), "元")
            valueTwoLab.attributedText = handlerDataAndMoney(String(format: "%.f", productModel.plstimeLimitValue.cGFloat), productModel.plstimeLimitType == "0" ? "天" : "月")
//            valueThreeLab.attributedText = handlerMoney(NSString(moneyNoPoint: productModel.investEnd) as String)
              valueThreeLab.attributedText = handlerMoney(String(format: "%.2f万", Double(productModel.investEnd)!/10000))
        }
    }
    func handlerProfit(_ str1: String, _ str2: String) -> NSMutableAttributedString {
        
        let str11 = String(format: "%.2f", str1.cGFloat * 100)
        
        let temp = NSMutableAttributedString(string: str11, attributes: [NSFontAttributeName: Font_WL(31)])
        temp.append(NSAttributedString(string: "%", attributes: [NSFontAttributeName: Font_WL(20)]))
        if str2.isEmpty {
            self.cashImg.image = UIImage(named:"")
            return temp
        }else{
            let str22 = String(format: "%.2f", str2.cGFloat * 100)
            temp.append(NSAttributedString(string: "+\(str22)%", attributes: [NSFontAttributeName: Font_WL(20)]))
            return temp
        }
    }
    func handlerDataAndMoney(_ str1: String, _ str2: String) -> NSMutableAttributedString {
        
        
        let temp = NSMutableAttributedString(string: str1, attributes: [NSFontAttributeName: Font_WL(17)])
        
        temp.append(NSAttributedString(string: "\(str2)", attributes: [NSFontAttributeName: Font_WL(11)]))
        return temp
        
    }
    func handlerMoney(_ str1: String) -> NSMutableAttributedString {
        
        
        let temp = NSMutableAttributedString(string: str1)
        
        temp.addAttributes([NSFontAttributeName : Font_WL(17)], range: NSRange(location: 0, length: str1.count-1))
        temp.addAttributes([NSFontAttributeName : Font_WL(11)], range: NSRange(location: str1.count-1, length: 1))
        return temp
        
    }
    func initUI() {
        
        self.addSubview(self.profitLab)
        _ = profitLab.sd_layout()
            .topSpaceToView(self, kJcwGetNavHeight()+10)?
            .heightIs(25)?
            .autoWidthRatio(1)?
            .centerXEqualToView(self)
        profitLab.setSingleLineAutoResizeWithMaxWidth(JCWSCREENW)
        
        /// 返现红包 或是 平台加息
        profitLab.addSubview(cashImg)
        _ = cashImg.sd_layout()
            .bottomSpaceToView(profitLab,20)?
            .rightSpaceToView(profitLab,-10)?
            .widthIs(55)?
            .heightIs(20)
        
        self.addSubview(self.titleLab)
        _ = titleLab.sd_layout()
            .topSpaceToView(profitLab,12)?
            .centerXEqualToView(profitLab)?
            .heightIs(11)?
            .autoWidthRatio(1)
        titleLab.setSingleLineAutoResizeWithMaxWidth(JCWSCREENW)
        
        
        self.addSubview(tagsView)
        _ = tagsView.sd_layout()
            .centerXEqualToView(self)?
            .offset(-tagsView.totoalWidth/2)?
            .topSpaceToView(titleLab,18)?
            .heightIs(15)
        
        
        
        for i in 0...2 {
            
            let menuView = MenuView()
            menuView.tag = 1001+i
            self.addSubview(menuView)
            
            _ = menuView.sd_layout().topSpaceToView(self,200-100 + kJcwGetNavHeight())?.leftSpaceToView(self,(JCWSCREENW * CGFloat(i))/3)?.heightIs(30)?.widthIs(JCWSCREENW/3)
            menuView.lineView.isHidden = i==2
        }
        let grayView = UIView()
        grayView.backgroundColor = UIColor.white
        self.addSubview(grayView)
        _ = grayView.sd_layout()
            .leftSpaceToView(self,0)?
            .rightSpaceToView(self,0)?
            .bottomSpaceToView(self,0)?
            .heightIs(120)
        
        self.addSubview(bottomLab)
        _ = bottomLab.sd_layout()
            .rightSpaceToView(self,15)?
            .bottomSpaceToView(self,8+7.5)?
            .leftSpaceToView(self,0)?
            .heightIs(10)
        
        self.addSubview(huikuanLab)
        _ = huikuanLab.sd_layout()
            .rightSpaceToView(self,15)?
            .bottomSpaceToView(bottomLab,65)?
            .heightIs(11)?
            .widthIs(60)
      
        let huiKuanImg = UIImageView()
        self.addSubview(huiKuanImg)
        huiKuanImg.backgroundColor = ColorFromRGBHex_WL(0x626262)
        _ = huiKuanImg.sd_layout()
            .topSpaceToView(huikuanLab,10)?
            .centerXEqualToView(huikuanLab)?
            .heightIs(6)?
            .widthIs(6)
        huiKuanImg.layer.cornerRadius = 3
        
        self.addSubview(huikuanValueLab)
        _ = huikuanValueLab.sd_layout()
            .bottomSpaceToView(huiKuanImg,-30)?
            .rightSpaceToView(self,15)?
            .heightIs(11)?
            .autoWidthRatio(1)
        huikuanValueLab.setSingleLineAutoResizeWithMaxWidth(JCWSCREENW/3)
        
      
      
        self.addSubview(jixiLab)
        _ = jixiLab.sd_layout()
            .centerXEqualToView(self)?
            .centerYEqualToView(huikuanLab)?
            .heightIs(11)?
            .widthIs(60)
        let jixiImg = UIImageView()
        jixiImg.backgroundColor = ColorFromRGBHex_WL(0x626262)
        self.addSubview(jixiImg)
        
        _ = jixiImg.sd_layout()
            .centerYEqualToView(huiKuanImg)?
            .centerXEqualToView(self)?
            .heightIs(6)?
            .widthIs(6)
        jixiImg.layer.cornerRadius = 3
        
        
        self.addSubview(jixiValueLab)
        _ = jixiValueLab.sd_layout()
            .centerYEqualToView(huikuanValueLab)?
            .centerXEqualToView(self)?
            .heightIs(11)?
            .autoWidthRatio(1)
        jixiValueLab.setSingleLineAutoResizeWithMaxWidth(JCWSCREENW/3)
        
        
        
    
        self.addSubview(todayLab)
        _ = todayLab.sd_layout()
            .leftSpaceToView(self,15)?
            .centerYEqualToView(huikuanLab)?
            .heightIs(11)?
            .widthIs(60)
        let todayImg = UIImageView()
        todayImg.backgroundColor = ColorFromRGBHex_WL(0x626262)
        self.addSubview(todayImg)
        _ = todayImg.sd_layout()
            .centerYEqualToView(huiKuanImg)?
            .centerXEqualToView(todayLab)?
            .heightIs(6)?
            .widthIs(6)
        todayImg.layer.cornerRadius = 3
        
        self.addSubview(todayValueLab)
        _ = todayValueLab.sd_layout()
            .centerYEqualToView(huikuanValueLab)?
            .leftSpaceToView(self,15)?
            .heightIs(11)?
            .autoWidthRatio(1)
        todayValueLab.setSingleLineAutoResizeWithMaxWidth(JCWSCREENW/3)
        
        let hengxian = UIView()
        hengxian.backgroundColor = ColorFromRGBHex_WL(0xC1A057)
        self.addSubview(hengxian)
        _ = hengxian.sd_layout()
            .rightSpaceToView(jixiImg,10)?
            .leftSpaceToView(todayImg,10)?
            .heightIs(0.5)?
            .centerYEqualToView(jixiImg)
        
        let hengxian2 = UIView()
        hengxian2.backgroundColor = ColorFromRGBHex_WL(0xC1A057)
        self.addSubview(hengxian2)
        _ = hengxian2.sd_layout()
            .leftSpaceToView(jixiImg,10)?
            .rightSpaceToView(huiKuanImg,10)?
            .heightIs(0.5)?
            .centerYEqualToView(jixiImg)
        let finalView = UIView()
        finalView.backgroundColor = Normal_458Color
        self.addSubview(finalView)
        _ = finalView.sd_layout()
            .bottomSpaceToView(self,0)?
            .rightSpaceToView(self,0)?
            .rightSpaceToView(self,0)?
            .heightIs(7.5)
        
    }
    
    /// 渐变的图片
    //    lazy var redImg: UIImageView = {
    //        let redImg = UIImageView()
    //
    //        return redImg
    //    }()
    /// 历史年化收益率
    lazy var profitLab :UILabel = {
        let profit = UILabel()
        profit.textColor = ColorFromRGBHex_WL(0xC1A057)
        profit.font = BoldFont_WL(31)
        profit.text = ""
        profit.textAlignment = .center
        return profit
    }()
    lazy var cashImg: UIImageView = {
        let temp = UIImageView()
        return temp
    }()
    lazy var tagsView: JCWActiveTagView = {
        let temp = JCWActiveTagView(ColorFromRGBHex_WL(0xFFEBC0),false, "")
        return temp
    }()
    /// 标题
    lazy var titleLab :UILabel = {
        let title = UILabel()
        title.textAlignment = .center
        title.textColor = UIColor.white
        title.font = Font_WL(11)
        title.text = "历史年化收益率"
        return title
    }()
    
    /// 底部的提示
    lazy var bottomLab: UILabel = {
        let temp  = UILabel()
        temp.textAlignment = .right
        temp.textColor = Normal_999Color
        temp.font = Font_WL(10)
        temp.text = "*具体日期以实际日期为准"
        return temp
    }()
    
    /// 开始回款日期
    lazy var huikuanValueLab: UILabel = {
        let temp  = UILabel()
        temp.textAlignment = .right
        temp.textColor = Normal_666Color
        temp.font = Font_WL(11)
        temp.text = ""
        return temp
    }()
    /// 开始回款
    lazy var huikuanLab: UILabel = {
        let temp  = UILabel()
        temp.textAlignment = .center
        temp.textColor = Normal_666Color
        temp.font = Font_WL(11)
        temp.text = "回款日"
        return temp
    }()
    /// 开始计息日期
    lazy var jixiValueLab: UILabel = {
        let temp  = UILabel()
        temp.textAlignment = .center
        temp.textColor = Normal_666Color
        temp.font = Font_WL(11)
        temp.text = ""
        return temp
    }()
    /// 开始计息
    lazy var jixiLab: UILabel = {
        let temp  = UILabel()
        temp.textAlignment = .center
        temp.textColor = Normal_666Color
        temp.font = Font_WL(11)
        temp.text = "计息日"
        return temp
    }()
    /// 今日投资日期
    lazy var todayValueLab: UILabel = {
        let temp  = UILabel()
        temp.textAlignment = .left
        temp.textColor = Normal_666Color
        temp.font = Font_WL(11)
        temp.text = ""
        return temp
    }()
    /// 开始计息
    lazy var todayLab: UILabel = {
        let temp  = UILabel()
        temp.textAlignment = .center
        temp.textColor = Normal_666Color
        temp.font = Font_WL(11)
        temp.text = "投资日"
        return temp
    }()
    
}

class MenuView : UIView{
    
    init() {
        super.init(frame: CGRect.zero)
        self.backgroundColor = UIColor.clear
        
        self.addSubview(lineView)
        _ = lineView.sd_layout()
            .centerYEqualToView(self)?
            .rightSpaceToView(self,0)?
            .widthIs(0.5)?
            .heightIs(26)
        self.addSubview(valueLab)
        valueLab.tag = 10002
        _ = valueLab.sd_layout()
            .centerYEqualToView(self)?
            .topSpaceToView(self,18)?
            .heightIs(17)?
            .widthIs(JCWSCREENW/3)
        
        self.addSubview(titleLab)
        titleLab.tag = 10001
        _ = titleLab.sd_layout()
            .centerYEqualToView(self)?
            .topSpaceToView(valueLab,5)?
            .heightIs(11)?
            .widthIs(JCWSCREENW/3)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    lazy var titleLab: UILabel = {
        let temp  = UILabel()
        temp.textAlignment = .center
        temp.textColor =  Color.white//Normal_666Color
        temp.font = Font_WL(11)
        temp.text = ""
        return temp
    }()
    lazy var valueLab: UILabel = {
        let temp  = UILabel()
        temp.textAlignment = .center
        temp.textColor = ColorFromRGBHex_WL(0xC1A057)
        temp.font = Font_WL(17)
        temp.text = ""
        return temp
    }()
    
    lazy var lineView: UIView = {
        let lineView = UIView()
        lineView.backgroundColor = Normal_CCCColor
        return lineView
    }()
}
